package com.bachelor.community.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.bachelor.community.annotation.UnInterception;
import com.bachelor.community.model.dto.UserDTO;
import com.bachelor.community.model.entity.User;
import com.bachelor.community.model.vo.UserVO;
import com.bachelor.community.service.intf.AnswerService;
import com.bachelor.community.service.intf.QuestionService;
import com.bachelor.community.service.intf.UserService;
import com.bachelor.community.utils.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private QuestionService questionService;
    @Resource
    private AnswerService answerService;

    @PostMapping
    public Result register(@RequestBody UserDTO userDTO) {
        String account = userDTO.getAccount();
        String password = userDTO.getPassword();
        if (StrUtil.isBlank(account) || StrUtil.isBlank(password)) {
            return Result.fail("400", "用户名或密码不能为空");
        } else {
            return Result.succeed("200", userService.register(userDTO));
        }
    }

    @GetMapping("{userId}")
    public Result getUser(@PathVariable Integer userId,
                          @RequestParam(value = "offset", defaultValue = "1") Integer currentPage,
                          @RequestParam(value = "limit", defaultValue = "10") Integer pageSize) {
        Map<String, Object> map = new HashMap<>(16);
        UserVO user = userService.getUserVOById(userId);
        Assert.notNull(user, "用户不存在");
        return Result.succeed("200", user);
    }

    @PatchMapping("{userId}")
    public Result partialUpdateUser(@PathVariable Integer userId, @RequestBody UserDTO userDTO) {
        return Result.succeed("200", userService.partialUpdateUser(userId, userDTO));
    }

    @GetMapping("/like")
    @UnInterception
    public Result listUsersOrderByLikeCount(@RequestParam(value = "offset", defaultValue = "1") Integer currentPage,
                                            @RequestParam(value = "limit", defaultValue = "10") Integer pageSize) {
        return Result.succeed("200", userService.listUsersOrderByLikeCount(currentPage, pageSize));
    }

    @GetMapping
    public Result listUsers(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                            @RequestParam(value = "classNumbers", required = false) List<String> classNumbers,
                            @RequestParam(value = "roles", required = false) List<String> roles,
                            @RequestParam(value = "orderBy", required = false) String creditField) {
        Page<User> page = new Page<>(currentPage, pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (classNumbers != null && !classNumbers.isEmpty()) {
            queryWrapper.in("class_number", classNumbers);
        }
        if (roles != null && !roles.isEmpty()) {
            queryWrapper.in("role", roles);
        }
        if (creditField != null) {
            queryWrapper.ne("role", 3);
            queryWrapper.orderByDesc(creditField);
        }
        userService.page(page, queryWrapper);
        page.convert(user -> {
            UserVO userVO = new UserVO();
            BeanUtil.copyProperties(user, userVO, true);
            return userVO;
        });
        return Result.succeed("200", page);
    }

    @GetMapping("classNumbers")
    public Result listClassNumbers() {
        return Result.succeed("200", userService.listClassNumbers());
    }
}
